package com.tencent.mm.ui.widget;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.animation.AnimationUtils;
import android.widget.FrameLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.view.ViewCompat;

import com.tencent.mm.R;
import com.tencent.mm.sdk.platformtools.ToolsAQ;
import com.tencent.mm.sdk.platformtools.ad;
import com.tencent.mm.ui.base.BaseB;
import com.tencent.mm.ui.mogic.MogicA;
import com.tencent.mm.ui.tools.ToolsJ;

public class SwipeBackLayout extends FrameLayout {
    public MogicA FQA;
    private float FQB;
    public Drawable FQC;
    private boolean FQD;
    private boolean FQE;
    public boolean FQF;
    public boolean FQG;
    public boolean FQH;
    private boolean FQI;
    private OnTouchListener FQJ;
    private Drawable FQK;
    private boolean FQL;
    private boolean FQM; // setNeedChangeWindowBackground
    public SwipeGestureDelegate FQN;
    private float FQx;
    private int FQy;
    private int FQz;
    private float QQ;
    private Rect Rs;
    public View XE; // self/this or child
    private boolean mEnable;
    private boolean mInLayout;

    public interface SwipeGestureDelegate {
        void onCancel();

        void onDrag();

        void onSwipeBack();
    }

    public interface OnTouchListener {
        void aL(float f);

        int bt(boolean z);

        void m(MotionEvent motionEvent);
    }

    private static class c extends MogicA.a implements BaseB.a {
        SwipeBackLayout sbl;
        int FQO;
        int FQP;
        int FQQ;

        private c(SwipeBackLayout layout) {
            this.sbl = layout;
            this.FQO = 0;
            this.FQP = 0;
            this.FQQ = 0;
        }

        @Override
        public int e(View view, int i) {
            if (this.sbl.FQF) {
                int max = Math.max(this.FQO, i);
                this.FQO = 0;
                return Math.min(view.getWidth(), Math.max(max, 0));
            }
            this.FQO = Math.max(this.FQO, i);
            return 0;
        }

        @Override
        public void a(View view, float f, float f2) {
            int width = view.getWidth();
            this.FQP = 0;
            this.FQQ = 0;
            width = (f > 0.0f || (f == 0.0f && this.sbl.FQB > this.sbl.FQx)) ? (width + this.sbl.FQC.getIntrinsicWidth()) + 10 : 0;
            this.FQP = width;
            ad.i("MicroMsg.SwipeBackLayout", "ashutest::onViewReleased, xvel:%f yvel:%f, releaseLeft:%d, releaseTop:%d, translucent %B", f, f2, this.FQP, this.FQQ, this.sbl.FQF);
            this.sbl.FQH = true;
            if (this.sbl.FQJ != null) {
                boolean z;
                OnTouchListener e = this.sbl.FQJ;
                if (this.FQP != 0) {
                    z = true;
                } else {
                    z = false;
                }
                width = e.bt(z);
                ad.i("MicroMsg.SwipeBackLayout", "intercepted by mSwipeBackListener.onViewReleased, result:%s", width);
                if (width == 2) {
                    this.FQP = 0;
                } else if (width == 3) {
                    return;
                }
            }
            if (this.sbl.FQF) {
                MogicA a = this.sbl.FQA;
                int i = this.FQP;
                int i2 = this.FQQ;
                if (a.TU) {
                    a.b(i, i2, (int) a.mVelocityTracker.getXVelocity(a.mActivePointerId), (int) a.mVelocityTracker.getYVelocity(a.mActivePointerId), 0);
                    this.sbl.invalidate();
                    return;
                }
                throw new IllegalStateException("Cannot settleCapturedViewAt outside of a call to Callback#onViewReleased");
            }
            this.sbl.FQI = true;
        }

        @Override
        public final void M(int i) {
            ad.i("MicroMsg.SwipeBackLayout", "ashutest::onViewDragStateChanged state %d, requestedTranslucent %B fastRelease %B", i, this.sbl.FQE, this.sbl.FQI);
            if (1 == i) {
                ad.i("MicroMsg.SwipeBackLayout", "ashutest:: on drag");
                if ((this.sbl.getContext() instanceof Activity) && this.sbl.FQK == null) {
                    this.sbl.FQK = ((Activity) this.sbl.getContext()).getWindow().getDecorView().getBackground();
                    if (this.sbl.FQM) {
                        ((Activity) this.sbl.getContext()).getWindow().getDecorView().setBackgroundResource(R.color.a_q);
                    }
                }
                if (this.sbl.FQN != null) {
                    this.sbl.FQN.onDrag();
                }
                this.sbl.FQG = false;
                if (this.sbl.FQF) {
                    WidgetH.bJ(0.0f);
                }
            }
            if (i == 0 && !this.sbl.FQI) {
                ad.i("MicroMsg.SwipeBackLayout", "ashutest:: on cancel");
                if (this.sbl.FQN != null) {
                    if ((this.sbl.getContext() instanceof Activity) && this.sbl.FQK != null) {
                        if (this.sbl.FQM) {
                            ((Activity) this.sbl.getContext()).getWindow().getDecorView().setBackground(this.sbl.FQK);
                        }
                        this.sbl.FQK = null;
                    }
                    this.sbl.FQN.onCancel();
                }
                WidgetH.bJ(1.0f);
            }
            if (1 == i && this.sbl.FQD && (this.sbl.getContext() instanceof Activity) && !this.sbl.FQF && !this.sbl.FQE) {
                ad.i("MicroMsg.SwipeBackLayout", "ashutest:: match dragging");
                this.sbl.FQE = true;
                BaseB.a((Activity) this.sbl.getContext(), this);
            }
            if (2 == i) {
                boolean z;
                ad.i("MicroMsg.SwipeBackLayout", "ashutest:: notify settle, mReleasedLeft %d", this.FQP);
                if (this.FQP > 0) {
                    z = true;
                } else {
                    z = false;
                }
                WidgetH.T(z, this.FQP);
            }
        }

        @Override
        public void hm(final boolean z) {
            ToolsAQ.f(new Runnable() {
                public final void run() {
                    c.this.sbl.FQE = z;
                    if (!z) {
                        c.this.sbl.FQH = false;
                    } else if (c.this.FQP > 0) {
                        WidgetH.bJ(0);
                    } else {
                        WidgetH.bJ(1.0f);
                    }
                    c.this.sbl.vk(z);
                    if (z && c.this.sbl.FQI) {
                        if (c.this.FQP == 0) {
                            ToolsJ.a(c.this.sbl.XE, 200, 0, new ToolsJ.a() {
                                public final void onAnimationEnd() {
                                    sbl.FQH = false;
                                }

                                public final void aTY() {
                                    onAnimationEnd();
                                }
                            });
                        } else {
                            ToolsJ.a(c.this.sbl.XE, 200, (float) c.this.FQP, new ToolsJ.a() {
                                public final void onAnimationEnd() {
                                    sbl.FQG = true;
                                    ToolsAQ.f(new Runnable() {
                                        public final void run() {
                                            if (sbl.FQN != null) {
                                                if ((sbl.getContext() instanceof Activity) && sbl.FQK != null) {
                                                    if (sbl.FQM) {
                                                        ((Activity) sbl.getContext()).getWindow().getDecorView().setBackground(sbl.FQK);
                                                    }
                                                    sbl.FQK = null;
                                                }
                                                sbl.FQN.onSwipeBack();
                                            }
                                            WidgetH.bJ(1.0f);
                                            sbl.FQH = false;
                                        }
                                    });
                                }

                                public final void aTY() {
                                    onAnimationEnd();
                                }
                            });
                            WidgetH.T(true, c.this.FQP);
                        }
                    }
                    c.this.sbl.FQI = false;
                }
            });
        }

        @Override
        public void js(int i, int i2) {
            if (this.sbl.FQF) {
                this.sbl.FQB = Math.abs(((float) i) / ((float) (this.sbl.XE.getWidth() + this.sbl.FQC.getIntrinsicWidth())));
                this.sbl.FQy = i;
                this.sbl.FQz = i2;
                this.sbl.invalidate();
                if (this.sbl.FQJ != null) {
                    this.sbl.FQJ.aL(this.sbl.FQB);
                }
                if (Float.compare(this.sbl.FQB, 1.0f) >= 0 && !this.sbl.FQG) {
                    this.sbl.FQG = true;
                    ToolsAQ.f(new Runnable() {
                        public final void run() {
                            if (sbl.FQN != null) {
                                if ((sbl.getContext() instanceof Activity) && sbl.FQK != null) {
                                    if (sbl.FQM) {
                                        ((Activity) sbl.getContext()).getWindow().getDecorView().setBackground(sbl.FQK);
                                    }
                                    sbl.FQK = null;
                                }
                                sbl.FQN.onSwipeBack();
                            }
                            sbl.FQH = false;
                        }
                    });
                } else if (Float.compare(this.sbl.FQB, 0.01f) <= 0) {
                    this.sbl.FQH = false;
                }
                if (this.sbl.FQA.TF == 1) {
                    WidgetH.bJ(this.sbl.FQB);
                }
            }
        }

        @Override
        public boolean XL(int i) {
            boolean z;
            MogicA a = this.sbl.FQA;
            if ((a.TN & (1 << i)) != 0) {
                z = true;
            } else {
                z = false;
            }
            if (!z || (a.TK[i] & 1) == 0) {
                return false;
            }
            return true;
        }

        @Override
        public final int eMO() {
            return 1;
        }
    }

    public SwipeBackLayout(@NonNull Context context) {
        this(context, null);
    }

    public SwipeBackLayout(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SwipeBackLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);

        this.FQx = 0.3f;
        this.mEnable = true;
        this.Rs = new Rect();
        this.FQD = true;
        this.FQE = false;
        this.FQF = false;
        this.FQG = false;
        this.FQH = false;
        this.FQI = false;
        this.FQM = true;
        this.FQN = null;
        this.FQC = getResources().getDrawable(R.drawable.b44);
        setFocusable(true);
        setDescendantFocusability(FOCUS_AFTER_DESCENDANTS);
        init();
    }

    public void init() {
        this.FQA = MogicA.a(this, new c(this), AnimationUtils.loadInterpolator(getContext(), R.anim.c0));
        this.FQA.TR = 1;
        float f = getResources().getDisplayMetrics().density;
        this.FQA.TP = 100.0f * f;
        this.FQA.TO = 300.0f * f;
        this.FQy = 0;
        this.FQz = 0;
    }

    public final void vk(boolean z) {
        this.FQF = z;
    }

    public void setNeedRequestActivityTranslucent(boolean z) {
        this.FQD = z;
        if (this.FQD) {
            this.FQE = false;
        }
    }

    public void setSwipeBackListener(OnTouchListener bVar) {
        this.FQJ = bVar;
    }

    public final boolean ekq() {
        eOF();
        return this.FQH;
    }

    public void setContentView(View view) {
        this.XE = view;
    }

    public View getTargetContentView() {
        return this.XE;
    }

    @Override
    public void onFinishInflate() {
        super.onFinishInflate();
        this.XE = this;
    }

    public void setEnableGesture(boolean z) {
        this.mEnable = z;
    }

    public void setOnceDisEnableGesture(boolean z) {
        this.FQL = z;
    }

    public final boolean eOF() {
        if (!this.FQH) {
            return false;
        } else if (Float.compare((float) this.XE.getLeft(), 0.01f) <= 0) {
            this.FQH = false;
            return false;
        } else {
            return true;
        }
    }


    @Override
    public boolean dispatchTouchEvent(MotionEvent motionEvent) {
        boolean dispatchTouchEvent;
        if (this.mEnable) {
            if (this.FQJ != null) {
                this.FQJ.m(motionEvent);
            }
            if (motionEvent.getAction() == 0) {
                this.FQL = false;
            }
            if (this.FQL) {
                dispatchTouchEvent = super.dispatchTouchEvent(motionEvent);
                return dispatchTouchEvent;
            } else if (eOF()) {
                dispatchTouchEvent = super.dispatchTouchEvent(motionEvent);
                return dispatchTouchEvent;
            } else {
                try {
                    int actionMasked;
                    int actionIndex;
                    float x;
                    View C;
                    float x2;
                    float y;
                    float f;
                    float y2;
                    boolean z;
                    if (this.FQA.TF == 1) {
                        MogicA aVar = this.FQA;
                        actionMasked = motionEvent.getActionMasked();
                        actionIndex = motionEvent.getActionIndex();
                        if (actionMasked == 0) {
                            aVar.cancel();
                        }
                        if (aVar.mVelocityTracker == null) {
                            aVar.mVelocityTracker = VelocityTracker.obtain();
                        }
                        aVar.mVelocityTracker.addMovement(motionEvent);
                        float y3;
                        int y4;
                        int left;
                        int left2;
                        switch (actionMasked) {
                            case MotionEvent.ACTION_DOWN:
                                x = motionEvent.getX();
                                y3 = motionEvent.getY();
                                actionIndex = motionEvent.getPointerId(0);
                                C = aVar.C((int) x, (int) y3);
                                aVar.a(x, y3, actionIndex);
                                aVar.z(C, actionIndex);
                                break;
                            case MotionEvent.ACTION_UP:
                                if (aVar.TF == 1) {
                                    aVar.fH();
                                }
                                aVar.cancel();
                                break;
                            case MotionEvent.ACTION_MOVE:
                                if (aVar.TF == 1) {
                                    actionMasked = motionEvent.findPointerIndex(aVar.mActivePointerId);
                                    y3 = motionEvent.getX(actionMasked);
                                    actionIndex = (int) (y3 - aVar.TI[aVar.mActivePointerId]);
                                    y4 = (int) (motionEvent.getY(actionMasked) - aVar.TJ[aVar.mActivePointerId]);
                                    left = aVar.TT.getLeft() + actionIndex;
                                    actionMasked = aVar.TT.getTop() + y4;
                                    left2 = aVar.TT.getLeft();
                                    int top = aVar.TT.getTop();
                                    if (actionIndex != 0) {
                                        left = aVar.FzJ.e(aVar.TT, left);
                                        aVar.TT.offsetLeftAndRight(left - left2);
                                    }
                                    if (y4 != 0) {
                                        aVar.TT.offsetTopAndBottom(0 - top);
                                        actionMasked = 0;
                                    }
                                    if (!(actionIndex == 0 && y4 == 0)) {
                                        aVar.FzJ.js(left, actionMasked);
                                    }
                                    aVar.f(motionEvent);
                                    break;
                                }
                                left = motionEvent.getPointerCount();
                                actionMasked = 0;
                                while (actionMasked < left) {
                                    actionIndex = motionEvent.getPointerId(actionMasked);
                                    x2 = motionEvent.getX(actionMasked);
                                    y = motionEvent.getY(actionMasked);
                                    f = x2 - aVar.TG[actionIndex];
                                    aVar.b(f, y - aVar.TH[actionIndex], actionIndex);
                                    if (aVar.TF != 1) {
                                        C = aVar.C((int) x2, (int) y);
                                        if (!aVar.u(C, f) || !aVar.z(C, actionIndex)) {
                                            actionMasked++;
                                        }
                                    }
                                    aVar.f(motionEvent);
                                    break;
                                }
                                aVar.f(motionEvent);
                                break;
                            case MotionEvent.ACTION_CANCEL:
                                if (aVar.TF == 1) {
                                    aVar.v(0.0f, 0.0f);
                                }
                                aVar.cancel();
                                break;
                            case MotionEvent.ACTION_POINTER_DOWN:
                                left = motionEvent.getPointerId(actionIndex);
                                x = motionEvent.getX(actionIndex);
                                y2 = motionEvent.getY(actionIndex);
                                aVar.a(x, y2, left);
                                if (aVar.TF != 0) {
                                    actionMasked = (int) x;
                                    actionIndex = (int) y2;
                                    C = aVar.TT;
                                    if (C != null && actionMasked >= C.getLeft() && actionMasked < C.getRight() && actionIndex >= C.getTop() && actionIndex < C.getBottom()) {
                                        aVar.z(aVar.TT, left);
                                    }
                                } else {
                                    aVar.z(aVar.C((int) x, (int) y2), left);
                                }
                                break;
                            case MotionEvent.ACTION_POINTER_UP:
                                actionIndex = motionEvent.getPointerId(actionIndex);
                                if (aVar.TF == 1 && actionIndex == aVar.mActivePointerId) {
                                    y4 = motionEvent.getPointerCount();
                                    for (actionMasked = 0; actionMasked < y4; actionMasked++) {
                                        left2 = motionEvent.getPointerId(actionMasked);
                                        if (left2 != aVar.mActivePointerId) {
                                            if (aVar.C((int) motionEvent.getX(actionMasked), (int) motionEvent.getY(actionMasked)) == aVar.TT && aVar.z(aVar.TT, left2)) {
                                                actionMasked = aVar.mActivePointerId;
                                                if (actionMasked == -1) {
                                                    aVar.fH();
                                                }
                                            }
                                        }
                                    }
                                }
                                aVar.bd(actionIndex);
                                break;
                        }
                        return true;
                    }
                    MogicA aVar2 = this.FQA;
                    actionMasked = motionEvent.getActionMasked();
                    int actionIndex2 = motionEvent.getActionIndex();
                    if (actionMasked == 0) {
                        aVar2.cancel();
                    }
                    if (aVar2.mVelocityTracker == null) {
                        aVar2.mVelocityTracker = VelocityTracker.obtain();
                    }
                    aVar2.mVelocityTracker.addMovement(motionEvent);
                    float y5;
                    switch (actionMasked) {
                        case MotionEvent.ACTION_DOWN:
                            x = motionEvent.getX();
                            y5 = motionEvent.getY();
                            actionIndex = motionEvent.getPointerId(0);
                            aVar2.a(x, y5, actionIndex);
                            View C2 = aVar2.C((int) x, (int) y5);
                            if (C2 == aVar2.TT && aVar2.TF == 2) {
                                aVar2.z(C2, actionIndex);
                            }
                            break;
                        case MotionEvent.ACTION_UP:
                        case MotionEvent.ACTION_CANCEL:
                            aVar2.cancel();
                            break;
                        case MotionEvent.ACTION_MOVE:
                            actionIndex2 = motionEvent.getPointerCount();
                            actionMasked = 0;
                            while (actionMasked < actionIndex2) {
                                actionIndex = motionEvent.getPointerId(actionMasked);
                                x2 = motionEvent.getX(actionMasked);
                                y = motionEvent.getY(actionMasked);
                                f = x2 - aVar2.TG[actionIndex];
                                aVar2.b(f, y - aVar2.TH[actionIndex], actionIndex);
                                if (aVar2.TF != 1) {
                                    C = aVar2.C((int) x2, (int) y);
                                    if (C == null || !aVar2.u(C, f) || !aVar2.z(C, actionIndex)) {
                                        actionMasked++;
                                    }
                                }
                                aVar2.f(motionEvent);
                                break;
                            }
                            aVar2.f(motionEvent);
                            break;
                        case MotionEvent.ACTION_POINTER_DOWN:
                            actionMasked = motionEvent.getPointerId(actionIndex2);
                            y2 = motionEvent.getX(actionIndex2);
                            y5 = motionEvent.getY(actionIndex2);
                            aVar2.a(y2, y5, actionMasked);
                            if (aVar2.TF != 0 && aVar2.TF == 2) {
                                View C3 = aVar2.C((int) y2, (int) y5);
                                if (C3 == aVar2.TT) {
                                    aVar2.z(C3, actionMasked);
                                    break;
                                }
                            }
                            break;
                        case MotionEvent.ACTION_POINTER_UP:
                            aVar2.bd(motionEvent.getPointerId(actionIndex2));
                            break;
                    }
                    if (aVar2.TF == 1) {
                        z = true;
                    } else {
                        z = false;
                    }
                    if (z) {
                        motionEvent.setAction(3);
                        super.dispatchTouchEvent(motionEvent);
                        return true;
                    }
                    super.dispatchTouchEvent(motionEvent);
                    return true;
                } catch (NullPointerException e) {
                    return false;
                } catch (IllegalArgumentException e2) {
                    return false;
                } catch (ArrayIndexOutOfBoundsException e22) {
                    return false;
                } catch (IllegalStateException e222) {
                    return false;
                }
            }
        }
        return super.dispatchTouchEvent(motionEvent);
    }

    protected void onLayout(boolean z, int i, int i2, int i3, int i4) {
        this.mInLayout = true;
        if (this.XE != null) {
            this.XE.layout(this.FQy, this.FQz, this.FQy + this.XE.getMeasuredWidth(), this.FQz + this.XE.getMeasuredHeight());
        }
        this.mInLayout = false;
    }

    public void requestLayout() {
        if (!this.mInLayout) {
            super.requestLayout();
        }
    }

    protected boolean drawChild(Canvas canvas, View view, long j) {
        Object obj = view == this.XE ? 1 : null;
        boolean drawChild = super.drawChild(canvas, view, j);
        if (!(Float.compare(this.QQ, 0.0f) <= 0 || obj == null || this.FQA.TF == 0)) {
            Rect rect = this.Rs;
            view.getHitRect(rect);
            this.FQC.setBounds(rect.left - this.FQC.getIntrinsicWidth(), rect.top, rect.left, rect.bottom);
            this.FQC.setAlpha((int) (this.QQ * 255.0f));
            this.FQC.draw(canvas);
        }
        return drawChild;
    }

    public void computeScroll() {
        Object obj;
        this.QQ = Math.max(0.0f, 1.0f - this.FQB);
        MogicA aVar = this.FQA;
        if (aVar.TF == 2) {
            boolean computeScrollOffset = aVar.lAx.computeScrollOffset();
            int currX = aVar.lAx.getCurrX();
            int currY = aVar.lAx.getCurrY();
            int left = currX - aVar.TT.getLeft();
            int top = currY - aVar.TT.getTop();
            if (left != 0) {
                aVar.TT.offsetLeftAndRight(left);
            }
            if (top != 0) {
                aVar.TT.offsetTopAndBottom(top);
            }
            if (!(left == 0 && top == 0)) {
                aVar.FzJ.js(currX, currY);
            }
            if (computeScrollOffset && currX == aVar.lAx.getFinalX() && currY == aVar.lAx.getFinalY()) {
                aVar.lAx.abortAnimation();
                computeScrollOffset = aVar.lAx.isFinished();
            }
            if (!computeScrollOffset) {
                aVar.TV.post(aVar.TW);
            }
        }
        if (aVar.TF == 2) {
            obj = 1;
        } else {
            obj = null;
        }
        if (obj != null) {
            ViewCompat.postInvalidateOnAnimation(this);
        }
    }

    public void setNeedChangeWindowBackground(boolean z) {
        this.FQM = z;
    }

    public void setSwipeGestureDelegate(SwipeGestureDelegate aVar) {
        this.FQN = aVar;
    }
}
